package com.xinli.xinlifm.view.fragment.forum;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.ForumBean;
import com.xinli.xinlifm.mvp.iview.MvpView;
import com.xinli.xinlifm.mvp.model.SuperModel;
import com.xinli.xinlifm.view.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XiaoLiu on 2016/10/13.
 */

public class Froum_NewFragment extends BaseFragment<SuperModel> implements MvpView<ForumBean>, SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView recyclerView;
    private FroumFragmentAdapter froumFragmentAdapter;
    private List<Object> list = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    private int offset = 0;
    private boolean flag = true;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    swipeRefreshLayout.setRefreshing(false);
                    break;
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.froum_two_choose_item,container,false);
    }

    @Override
    public SuperModel getModel() {
        return new SuperModel();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerView = ((RecyclerView) view.findViewById(R.id.forum_recyclerView));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.forum_refresh);
        swipeRefreshLayout.setOnRefreshListener(this);
        froumFragmentAdapter = new FroumFragmentAdapter(getContext(),list);
        recyclerView.setAdapter(froumFragmentAdapter);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        getModel().getForumData(0, offset ,10 ,0);  //最新是0
    }

    @Subscribe
    @Override
    public void showView(ForumBean data) {
        if(offset != 0 || flag){
            flag = false;
            froumFragmentAdapter.addAll(data.getData());
        }
        Log.e("自定义标签", "类名==ForumFragment" + "方法名==showView=====:" + data);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        //   getModel().getForumData(0, 1 ,limit ,1);
        handler.sendEmptyMessageDelayed(100,1000);
    }
}
